package com.ssy.lingxi.contract.utils;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.ssy.lingxi.contract.model.bo.sign.ContractParamFillBO;
import com.ssy.lingxi.contract.model.bo.sign.ContractParamMemberFillBO;
import com.ssy.lingxi.contract.model.bo.sign.OrderBO;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;

@Slf4j
public class ContractTemplateUtil {

    private ContractTemplateUtil() {

    }

    public static byte[] fillContractTemplate(byte[] bytes, ContractParamFillBO contractParamFillBO) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

        // 填充合同参数
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();

        // 甲方信息
        ContractParamMemberFillBO leadingContractFillParam = contractParamFillBO.getLeadingContractFillParam();
        // 乙方信息
        ContractParamMemberFillBO laborContractFillParam = contractParamFillBO.getLaborContractFillParam();
        // 订单信息
        OrderBO orderBO = contractParamFillBO.getOrderBO();

        try {
            ByteArrayOutputStream docbaos = new ByteArrayOutputStream();
            Configure build = Configure.newBuilder()
                    .buildGramer("${", "}")
                    .bind("goods", policy)
                    .build();
            XWPFTemplate template = XWPFTemplate.compile(bais, build).render(
                    new HashMap<String, Object>() {{
                        put("buyerName", leadingContractFillParam.getOrgName());
                        put("buyerRepName", leadingContractFillParam.getRepName());
                        put("buyerMobile", leadingContractFillParam.getMobile());
                        put("buyerAddress", leadingContractFillParam.getAddress());
                        put("buyerContractName", contractParamFillBO.getBuyerContractName());
                        put("buyerContractMobile", contractParamFillBO.getBuyerContractMobile());
                        put("buyerReceiveAddress", contractParamFillBO.getBuyerReceiveAddress());

                        put("supplierName", laborContractFillParam.getOrgName());
                        put("supplierRepName", laborContractFillParam.getRepName());
                        put("supplierMobile", laborContractFillParam.getMobile());
                        put("supplierAddress", laborContractFillParam.getAddress());
                        put("supplierBank", laborContractFillParam.getBankName());
                        put("supplierAccount", laborContractFillParam.getBankAccount());
                        put("supplierCardNo", laborContractFillParam.getBankCardNo());

                        put("goods", orderBO.getOrderProductList());
                        put("productAmountWord", orderBO.getProductAmountWord());
                        put("freightAmountWord", orderBO.getFreightAmountWord());
                        put("totalAmountWord", orderBO.getTotalAmountWord());
                    }});
            template.write(docbaos);
            template.close();

            return docbaos.toByteArray();
        } catch (Exception e) {
            log.error("填充合同参数", e);
        }
        return null;
    }
}
